package com.v2future.sjt.collegecheckingattendance.utils.netutils;

import org.xutils.common.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by minnie on 16/1/7.
 */
public abstract class NetTaskListener<T> {

    /**
     * 数据获取完成
     * @param result
     */
    public abstract void onSuccess(T result);

    /**
     * 位置网络错误
     */
    public abstract void onError();

    /**
     * 非正常状态的数据
     * @param result
     */
    public abstract void onInterError(T result);

    /**
     * 网络错误
     * @param responseCode
     * @param responseMsg
     * @param errorResult
     */
    public abstract void onNetError(int responseCode, String responseMsg, String errorResult);

    /**
     * 请求取消
     */
    public abstract void onCancelled();

    /**
     * 网络请求流程结束
     */
    public abstract void onFinished();

    /**
     * 缓存获取完成
     * @param result
     */
    public abstract void onSuccessCache(T result);

    private Type mType;

    public NetTaskListener() {
        mType = getSuperclassTypeParameter(getClass());

    }

    public Type getType() {
        return mType;
    }

    private Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return parameterized.getActualTypeArguments()[0];
    }
}
